<?php

namespace App\Models;

use Illuminate\Support\Arr;

class AdminMenuModel extends Model {
    //use SoftDeletes;
    /**
     * @var string
     */
    protected $table = 'sys_admin_menu';

    protected $primaryKey = 'id';

    public $incrementing = true;

    protected $guarded = [];

    const UPDATED_AT = null; //更新不需要这个字段可以设置为null

    const CREATED_AT = null; //更新不需要这个字段可以设置为null

    /**
     * @param $SystemType 隶属系统
     */
    public static function getPermissionTreeList($SystemType)
    {
        $where['systemType'] = $SystemType;
        $permissionList = self::select('id', 'systemType', 'menuName', 'webUrl', 'parentId', 'sort', 'menuType', 'keyword', 'icoClass', 'status', "webUrl")->where($where)
            ->orderBy('createTime', 'asc')
            ->get()
            ->toArray();
        $permissionArr = self::createTreeList($permissionList, 0, 0);

        return $permissionArr;
    }

    /**
     * 产生树列表最终返回的树状列表
     * @param $record_list
     * @param $parent_id
     * @param $level
     */
    public static function createTreeList($record_list, $parent_id, $level)
    {
        $record_arr = [];
        foreach ($record_list as $key => $value) {
            if ($value['parentId'] == $parent_id) {
                $value['level'] = $level + 1;
                $value['children'] = self::createTreeList($record_list, $value['id'], $value['level']);
                if (empty($value['children'])) {
                    unset($value['children']);
                }
                $record_arr[] = $value;
            }
        }
        return $record_arr;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 获取上级菜单信息
     */
    function getMenuInfo()
    {
        $select = ['id', 'systemType', 'menuName', 'parentId', 'icoClass', 'sort', 'status', 'menuType', 'webUrl', 'keyword'];
        return $this->hasOne(AdminMenuModel::class, 'id', 'parentId')->select($select);
    }

    /**
     * @param $roleIds  角色的id
     * @param $deptIds  部门id
     * @return array
     * 获取权限菜单
     */
    public static function getPermissionParentChildrenList($roleIds, $deptIds)
    {
        $permission_arr = array();
        $child_permission_arr = array();
        $ids_arr = array();
        if (!empty($roleIds)) {
            $role_ids_arr = AdminRoleMenuMapModel::getPermissionArr($roleIds);

            if ($role_ids_arr) {
                $ids_arr = array_merge($ids_arr, $role_ids_arr);
            }
        }
        if (!empty($deptIds)) {
            $dept_ids_arr = AdminDeptMenuMapModel::getPermissionArr($deptIds);
            if ($dept_ids_arr) {
                $ids_arr = array_merge($ids_arr, $dept_ids_arr);
            }
        }
        //菜单
        $permissionList = self::select('id', 'systemType', 'menuName', 'parentId', 'menuType', 'keyword', 'icoClass', 'webUrl')
            ->where('isDelete', 0)
            ->where('status', 1)->where('menuType', 1)
            ->where('systemType', 1) //此条件为系统，如果是多个系统在共用一张数据表，那就使用字段做区分
            ->whereIn("id", $ids_arr)
            ->orderBy('sort', 'desc')
            ->get()
            ->toArray();
        //操作
        $operatingList = self::select('id', 'systemType', 'menuName', 'parentId', 'menuType', 'keyword', 'icoClass', 'webUrl')
            ->where('isDelete', 0)
            ->where('status', 1)->where('menuType', 2)
            ->where('systemType', 1) //此条件为系统，如果是多个系统在共用一张数据表，那就使用字段做区分
            ->orderBy('sort', 'desc')
            ->get()
            ->toArray();
        $operatingMap = [];
        foreach ($operatingList as $val) {
            $operatingMap[$val["parentId"]][$val["webUrl"]] = $val;
        }

        //记入一级菜单
        foreach ($permissionList as $row) {
            if (empty($row['parentId'])) {
                if (in_array($row['id'], $ids_arr)) {
                    $permission_arr[$row['id']] = $row;
                    $permission_arr[$row['id']]['operatingArr'] = !empty($operatingMap[$row["id"]]) ? $operatingMap[$row["id"]] : [];
                    $permission_arr[$row['id']]["mate"] = ["icon" => $row["icoClass"], "title" => $row["menuName"]];
                    $permission_arr[$row['id']]['children'] = [];
                }
            }
        }
        //记入二级菜单
        foreach ($permissionList as $row) {
            if (!empty($row['parentId'])) {
                if (in_array($row['id'], array_unique($ids_arr))) {
                    if (!empty($permission_arr[$row['parentId']])) {
                        $permission_arr[$row['parentId']]['children'][$row['id']] = $row;
                        $permission_arr[$row['parentId']]['children'][$row['id']]["mate"] = ["icon" => $row["icoClass"], "title" => $row["menuName"]];
                        $permission_arr[$row['parentId']]['children'][$row['id']]['operatingArr'] = !empty($operatingMap[$row["id"]]) ? $operatingMap[$row["id"]] : [];
                        $permission_arr[$row['parentId']]['children'][$row['id']]['children'] = [];
                    }
                }
            }
        }

        $permission_arr = array_map(function ($val) {
            $val['children'] = array_values($val['children']);
            return $val;
        }, $permission_arr);

        return array_values($permission_arr);
    }


}
